package com.s10.ecloud.web.dao.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import com.s10.ecloud.web.dao.IUserDAO;
import com.s10.ecloud.web.model.EnterpriseUser;
import com.s10.ecloud.web.model.IndividualUser;
import com.s10.ecloud.web.model.Role;
import com.s10.ecloud.web.model.SystemUser;
import com.s10.ecloud.web.model.User;

@Repository
public class UserDAO<U extends User> implements IUserDAO<U> {

	@Resource
	private SessionFactory sessionFactory;

	public List<U> findAllUser() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public User findUserbyId(User user) {
		{
			Object o = sessionFactory.getCurrentSession().get(user.getClass(),
					user.getId());
			if (user != null) {
				return (U) o;
			}
			return null;
		}
	}

	public List<User> findUserByEMail(String eMail) {
		List<User> list = new ArrayList<User>();

		list.addAll(this.findEntUserByEMail(eMail));
		list.addAll(this.findIndUserByEMail(eMail));
		list.addAll(this.findSysUserByEMail(eMail));

		return list;

	}

	public List<User> findEntUserByEMail(String eMail) {

		String hql = "from EnterpriseUser where EMAIL = :email";
		Query query = sessionFactory.getCurrentSession().createQuery(hql);
		query.setParameter("email", eMail);

		List<User> list = query.list();

		if (list != null)
			return list;
		else
			return new ArrayList<User>();
	}

	public List<User> findSysUserByEMail(String eMail) {

		String hql = "from SystemUser where EMAIL = :email";
		Query query = sessionFactory.getCurrentSession().createQuery(hql);
		query.setParameter("email", eMail);

		List<User> list = query.list();

		if (list != null)
			return list;
		else
			return new ArrayList<User>();
	}

	public List<User> findIndUserByEMail(String eMail) {

		String hql = "from IndividualUser where EMAIL = :email";
		Query query = sessionFactory.getCurrentSession().createQuery(hql);
		query.setParameter("email", eMail);

		List<User> list = query.list();

		if (list != null)
			return list;
		else
			return new ArrayList<User>();
	}

	@Override
	public User updateUser(User user) {

		sessionFactory.getCurrentSession().update(user);
		return this.findUserbyId(user);
	}

	public List<User> getEntUserByCommerceNo(String commerceNo) {

		String hql = "from EnterpriseUser where commerceNo = :commerceNo";
		Query query = sessionFactory.getCurrentSession().createQuery(hql);
		query.setParameter("commerceNo", commerceNo);

		List<User> list = query.list();

		if (list != null)
			return list;
		else
			return new ArrayList<User>();
	}

	public String userLogin(U user) {
		// TODO Auto-generated method stub
		return null;
	}

	public SystemUser findUserbyId(Integer id) {
		// TODO Auto-generated method stub
		return null;
	}

	public boolean existUser(String username) {
		// TODO Auto-generated method stub
		return false;
	}

	public void addUser(U user) {
		sessionFactory.getCurrentSession().save(user);

	}

	public SystemUser findUserbyUsername(String username) {
		// TODO Auto-generated method stub
		return null;
	}

	public U findUser(U user) {

		return null;
	}

	public void freezeUserstateByUsername(String username) {
		// TODO Auto-generated method stub

	}

	public void recoverUserstateByUsername(String username) {
		// TODO Auto-generated method stub

	}

	public void updateUserRole(String username, Role role) {
		// TODO Auto-generated method stub

	}

	public String updatePassword(int userId, String password) {
		// TODO Auto-generated method stub
		return null;
	}

	public List<U> findAllUserByDep() {
		// TODO Auto-generated method stub
		return null;
	}

	public List<U> findUsersByRole(Role role) {
		// TODO Auto-generated method stub
		return null;
	}

}
